<script setup lang="ts">
import {articleStatusOptions, statusOpts} from "@/utils/status";
import {ArticleForm, ArticleQuery, deleteArticle, getArticleList} from "@/api/article";
import defaultSettings from "@/settings";

defineOptions({
  name: "Article",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);
const objFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const lists = ref<ArticleForm[]>();
const editVisible = ref(false);
const currentId = ref<number | undefined>();

const statusOptions = articleStatusOptions();
const queryParams = reactive<ArticleQuery>({
  pageNum: 1,
  pageSize: 10,
});


/** 查询 */
function handleQuery() {
  loading.value = true;
  getArticleList(queryParams)
    .then(({data}) => {
      lists.value = data.items || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;

  handleQuery();
}

/**
 * 打开弹窗
 *
 * @param type 弹窗类型  用户表单：user-form | 用户导入：user-import
 * @param id 用户ID
 */
async function openDialog(id?: number) {
  editVisible.value = true;

  if (id)
    currentId.value = id;
}

/** 删除 */
function handleDelete(id: number) {
  if (!id) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中记录?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    deleteArticle(id)
      .then(() => {
        ElMessage.success("删除成功");
        resetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

async function closeDialog(type?: any) {
  editVisible.value = false;
  currentId.value = undefined;

  if (type == 1) {
    handleQuery();
  }
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form
        class="pull-left"
        ref="queryFormRef"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item prop="keywords" label="">
          <el-input v-model="queryParams.keywords" placeholder="文章名称" clearable @keyup.enter="handleQuery"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search/>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh/>
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-button v-hasPerm="['article:add']" type="success" @click="openDialog()" class="pull-right">
        <i-ep-plus/>
        新增
      </el-button>
      <div class="clear"></div>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="lists">
        <el-table-column label="编号" align="center" prop="id" width="60"/>
        <el-table-column label="分类名称" align="center" prop="category_title"/>
        <el-table-column label="标题" align="center" prop="title"/>
        <el-table-column label="预览图" align="center" prop="thumb">
          <template #default="scope">
            <el-image :src="defaultSettings.imgDomain + scope.row.thumb" style="width: 45px; height: 45px;" v-if="scope.row.thumb"/>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort"/>
        <el-table-column label="创建时间" align="center" prop="create_time"/>
        <el-table-column label="状态" align="center" width="160">
          <template #default="scope">
            <el-tag :type="scope.row.status == 2 ? 'success' : 'info'">
              {{ statusOptions[scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="160">
          <template #default="scope">
            <el-button v-hasPerm="['article:edit']" type="primary" size="small"
                       :disabled="scope.row.shop_type == 0 ? true: false"
                       link @click="openDialog(scope.row.id)">
              <i-ep-edit class="margin3-r"/>
              编辑
            </el-button>
            <el-button v-hasPerm="['article:delete']" type="danger" size="small"
                       :disabled="scope.row.shop_type == 0 ? true: false"
                       link @click="handleDelete(scope.row.id)">
              <i-ep-delete class="margin3-r"/>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 编辑表单弹窗 -->
    <editArticle
      :visible="editVisible"
      :articleId="currentId"
      width="1000px"
      @close="closeDialog"
    />

  </div>
</template>
